<template>
  <div class="login-wrap">
    <el-card class="login-container">
      <div class="login-title" slot="header">个人博客后台管理系统</div>
      <!-- 登录form -->
      <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="danger" @click="reset">重置</el-button>
          <el-button type="success" @click="enroll">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    reset() {
      this.$refs['loginForm'].resetFields()
    },
    enroll() {
      this.$router.push('/enroll')
    },
    login() {
      this.$api.postLogin(this.form).then((result) => {
        const { id, code, token, message } = result.data
        if (code == 0) {
          localStorage.setItem('token', token)
          this.$message.success(message)
          this.$router.push('/')
        } else {
          this.$message.error(message)
        }
      })
    },
  },
}
</script>

<style>
.login-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_1280.jpg')
    no-repeat;
  background-size: 100%;
  /* background-color: #27304f; */
}
.login-container {
  width: 400px;
  /* height: 200px; */
  color: #ccc;
  background-color: rgba(0, 0, 0, 0.2);
}
.el-form label {
  color: #ccc;
}
.login-title {
  text-align: center;
}
/* .test-demo {
  width: 100px;
} */
</style>
